package 
{
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Mosquito extends MovieClip
	{
		private var x1:int
		private var x2:int
		private var y1:int
		private var y2:int
		
		private var xI:int
		private var yI:int
		private var xF:int
		private var yF:int
		
		private var angulo:int
		
		private var tweenX:Tween;
		private var tweenY:Tween;
		
		private const RADIO_MOVIMIENTO:int = 50;
		
		private var context:Escenario
		
		public function Mosquito(x1:int, y1:int, x2:int, y2:int, context:Escenario) {
			
			this.x1 = x1
			this.x2 = x2
			this.y1 = y1
			this.y2 = y2
			
			this.context = context
			
			x = Math.round(Math.random() * (x2 - x1 - width)) + x1;
			y = Math.round(Math.random() * (y2 - y1 - height)) + y1;

			xI = x
			yI = y

		}
		
		public function run() {
			
			var angulo:int 
			trace("xI " + xI+ " yI" + yI)
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x00FF00, 1);
			spr.graphics.drawRect(xI,yI,2,2);
			spr.graphics.endFill();

			context.addChild(spr);
			
			while (!isZone()) {
				angulo = Math.round((Math.random() * 360))

				rotation = angulo + 45
				angulo = (angulo * Math.PI) / 180
				
				
				xF = (xI + Math.round(RADIO_MOVIMIENTO * Math.cos(angulo))) - width/2
				yF = (yI + Math.round(RADIO_MOVIMIENTO * Math.sin(angulo))) - height/2
			}
			
			trace("xF: " + xF + " yF" +yF)
			/*
			
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x00FF00, 1);
			spr.graphics.drawRect(xF,yF,2,2);
			spr.graphics.endFill();

			context.addChild(spr);
			*/
			
			/*if ( tweenX != null )
				tweenX.removeEventListener(TweenEvent.MOTION_FINISH, parar);
				*/
			tweenX = new Tween(this, "x", null, this.x, xF, 4, true);
			tweenY = new Tween(this, "y", null, this.y, yF, 4, true);
			
			tweenX.start()
			tweenY.start()
			
			tweenY.addEventListener(TweenEvent.MOTION_FINISH, parar);
			//tweenY.addEventListener(TweenEvent.MOTION_CHANGE, colision);
		}
		
		private function colision(e:TweenEvent):void {
			trace("hola")

		}
		
		/*
		 * 
		 * Esta funcion determina sii el punto generado aleatoriamente esta dentro de la zona 
		 * cubierta por los mosquitos.
		 * */
		private function isZone():Boolean {
			
			if (xF >= x1 && xF <= (x2 - width)  && yF >= y1 && yF <= (y2 - height)) {
				return true
			}
			else {
				return false
			}
		}
		
		private function parar(e:TweenEvent):void
		{
			//trace("parar")
			//gotoAndStop(0);
			xI = xF
			yI = yF
			x = xI
			y = yI
			xF = 0
			yF=0
			run()
		}
		
		public function getX():int {
			return x
		}
		
		public function getY():int {
			return y
		}
		
		public function setY(y:int) {
			this.y = y
		}
		
		public function setX(x:int) {
			this.x = x
		}
		
		public function getAnngulo():int {
			return angulo
		}
		
		public function setAngulo(angulo:int) {
			this.angulo = angulo
		}
		
		
		
	}
	
}